﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class ITBusinessOverviewController : BaseController
    {
        private ISiteBranch siteBranch;
        private IUser_X user;
        private IBusinessSystem businessSystem;

        [InjectionConstructor]
        public ITBusinessOverviewController(ISiteBranch siteBranch, IUser_X user, IBusinessSystem businessSystem)
        {
            this.siteBranch = siteBranch;
            this.user = user;
            this.businessSystem = businessSystem;
        }
        protected override void Dispose(bool disposing)
        {
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)user).Dispose();
            ((IDisposable)businessSystem).Dispose();
        }

        public ActionResult Index()
        {
            base.SetRoles(user);
            List<SiteBranchModel> sites = (from c in siteBranch.GetSiteALLBranch()
                                           where UserSiteIDs.Contains(c.ID)
                                           select c).ToList();
            //权限控制
            if (sites.Count == 0) return new HttpStatusCodeResult(403);

            decimal siteId = sites.FirstOrDefault().ID;
           
            ViewData["SiteID"] = siteId;
            ViewBag.Sites = sites;
            
            return View();
        }

        public ActionResult GetSearch(string siteId)
        {
            base.SetRoles(user);
            decimal sid = Convert.ToDecimal(siteId);
            //权限控制
            if (!UserSiteIDs.Contains(sid)) return new HttpStatusCodeResult(403);

            List<BusinessSystemModel> bSystemList = (from c in businessSystem.GetAllBusinessSystem()
                                                     where c.SiteID == sid
                                                     select c).OrderBy(q=>q.Name).ToList();
            ViewBag.SystemList = bSystemList;
            return PartialView();
        }

        public ActionResult GetCharts(string select, int siteid)
        {
            base.SetRoles(user);
            //权限控制
            if (!UserSiteIDs.Contains(siteid)) return new HttpStatusCodeResult(403);

            String[] methods = null;
            Type incr = typeof(IncidentController);
            String[] inmethods = incr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&Incident").ToArray();
            Type sdcr = typeof(ServiceDeskController);
            String[] sdmethods = sdcr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&ServiceDesk").ToArray();
            Type chcr = typeof(ChangeController);
            String[] chmethods = chcr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&Change").ToArray();
            Type prcr = typeof(ProblemController);
            String[] prmethods = prcr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&Problem").ToArray();
           

            switch (select)
            {
                case "Incident":
                    methods = new String[inmethods.Length];
                    inmethods.CopyTo(methods, 0);
                    break;
                case "Desk":
                    methods = new String[sdmethods.Length];
                    sdmethods.CopyTo(methods, 0);
                    break;
                case "Change":
                    methods = new String[chmethods.Length];
                    chmethods.CopyTo(methods, 0);
                    break;
                case "Problem":
                    methods = new String[prmethods.Length];
                    prmethods.CopyTo(methods, 0);
                    break;
                //case "Configuration": //TODO
                //    methods = new String[sdmethods.Length];
                //    sdmethods.CopyTo(methods, 0);
                //    break;
                //case "SLA": //TODO
                //    methods = new String[sdmethods.Length];
                //    sdmethods.CopyTo(methods, 0);
                //    break;
                case "All":
                    methods = new String[inmethods.Length + sdmethods.Length + chmethods.Length + prmethods.Length];
                    inmethods.CopyTo(methods, 0);
                    sdmethods.CopyTo(methods, inmethods.Length);
                    chmethods.CopyTo(methods, inmethods.Length + sdmethods.Length);
                    prmethods.CopyTo(methods, inmethods.Length + sdmethods.Length + chmethods.Length);
                    break;
                default:
                    return new HttpStatusCodeResult(403);
            }

            return PartialView(methods);
        }
    }
}
